<?php

namespace app\common\model;

use think\Db;
use think\Model;

/**
 * 用户组明细表模型
 * Class GroupAccess
 * @package app\common\model
 */
class GroupAccess extends Model
{
    //数据表
    protected $table = 'authgroupaccess';

    public function setAuth($data){
        //进行验证
        if(!isset($data['group_id'])){
            return ['valid'=>0,'msg'=>'请勾选用户组'];
        }

        //3.进行编辑之先删添
        if (db('authgroupaccess')->find($data['uid'])) {
            $this->where('uid',$data['uid'])->delete();
        }

        //第一次是添加  后面都是编辑的添加
        foreach($data['group_id'] as $v){
            $newData = [
                'uid'=>$data['uid'],
                'group_id'=>$v
            ];
            Db::table('authgroupaccess')->insert($newData);
        }
        return ['valid'=>1,'msg'=>'设置成功'];
    }

}
